var express = require('express');
var router = express.Router();
var { loginModel } = require('../model/model')

/* GET home page. */
router.get('/', function (req, res, next) {
    res.render('index', { title: 'Express' });
});

router.get('/showuser', async (req, res) => {
    let { user, password } = req.query
    console.log(user, password);

    let data = await loginModel.findOne({ user: user, password: password })
    if (data) {
        res.send({
            code: 200,
            data
        })
    } else {
        res.send({
            code: 400,
            msg: '用户名或密码错误'
        })
    }
})

router.get('/showusers', async (req, res) => {
    let { tel } = req.query
    let data = await loginModel.findOne({ phone: tel })

    if (data) {
        res.send({
            code: 200,
            data
        })
    } else {
        res.send({
            code: 400,
            msg: '用户名或密码错误'
        })
    }
})

router.post('/adduser', async (req, res) => {
    let body = req.body
    await loginModel.create(body)
    res.send({
        code: 200,
        msg: '添加用户成功'
    })
})


module.exports = router;
